package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.atguigu.srb.result.ResponseVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@Api(tags = "借款信息的接口")
@RestController
@RequestMapping("/api/core/borrowInfo")
public class ApiBorrowInfoController {
    @Resource
    private BorrowInfoService borrowInfoService;

    @ApiOperation("获取最新一次提交的借款的状态")
    @GetMapping("status")
    public ResponseVo status(@RequestHeader("token") String token){
        Integer status = borrowInfoService.status(token);
        return  ResponseVo.ok().addData("item",status);
    }

    @ApiOperation("撤销借款申请")
    @PutMapping("cancel/{borrowId}")
    public ResponseVo cancelBorrowRequest(@RequestHeader("token") String token, @PathVariable Long borrowId){
        // 前提：用户应该要登陆
        borrowInfoService.cancelBorrowRequest(token,borrowId);
        return ResponseVo.ok().message("撤销成功");
    }
    @ApiOperation("获取当前用户的借款信息列表")
    @GetMapping("/{pageNum}/{pageSize}")
    public ResponseVo getBorrowInfoListByToken(@RequestHeader("token") String token,
                                               @PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Page<BorrowInfo> page = new Page<>(pageNum, pageSize);
        borrowInfoService.getBorrowInfoListByToken(token,page);
        return ResponseVo.ok().addData("page",page);
    }
    @ApiOperation("借款信息的保存")
    @PostMapping
    public ResponseVo save(@RequestHeader("token") String token, @RequestBody BorrowInfo borrowInfo){
        borrowInfoService.saveBorrowInfo(token,borrowInfo);
        return  ResponseVo.ok();
    }
}

